package com.ruoyi.common.utils.Mail;


import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.math.BigDecimal;
import java.security.GeneralSecurityException;
import java.util.Properties;
import java.util.Random;

public class MailUtils {
	/**
	 * 发�?�邮�?
	 * 
	 * @param form 发件�?
	 * @param to   收件�?
	 * @param subject 标题
	 * @param text 内容
	 * @throws AddressException
	 */
	public static void sendMail(String form, String to,String subject, String text) throws javax.mail.MessagingException, GeneralSecurityException {
		Properties prop = new Properties();
		MailSSLSocketFactory sf=new MailSSLSocketFactory();
		sf.setTrustAllHosts(true);
		prop.put("mail.smtp.ssl.socketFactory", sf);
		prop.put("mail.transport.protocol", "smtp");// 连接协议
		prop.put("mail.smtp.host", "smtp.qq.com");// 主机�?
		prop.put("mail.smtp.port", 465);// 端口�?
		prop.put("mail.smtp.auth", "true");//wsdxyezvdbdldjda
		prop.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---�?般都使用
		prop.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信�?
		prop.put("mail.smtp.socketFactory.port", 465);
		// 创建session
		Session session = Session.getInstance(prop);

		// 创建邮件
		MimeMessage message = new MimeMessage(session);

		// 发件人邮箱地�?
		message.setFrom(new InternetAddress(form));

		// 收件人邮箱地�?
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

		// 邮件标题
		message.setSubject(subject);

		// 邮件内容
		message.setText(text);

		// 通过session得到transport对象
		Transport transport = session.getTransport();

		// 连接自己的邮箱账�?
		transport.connect("1590648573@qq.com", "upnyxlxobgcchddg");// 密码为QQ邮箱�?通的stmp服务后得到的客户端授权码


		// 发�?�邮�?
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();

	}
	public static int register(String supplier,String orderNo,BigDecimal money,String email) {
		try {
			Random ra=new Random();
			int result = 0;
			for (int i=0;i<6;i++)
			{
				result+=ra.nextInt(100000);
			}
			String subject="来自物流系统的提示";
			//邮箱内容
			String text ="供应商："+supplier+"\n"+"提醒：有一条待发货的订单，订单号为"+orderNo+"\n"+"应收："+money+"元";
			//发送邮件
			MailUtils.sendMail("1590648573@qq.com",email,subject, text);
			return result;
		} catch (AddressException e) {
			e.printStackTrace();
			return 0;
		} catch (javax.mail.MessagingException e) {
			e.printStackTrace();
			return 0;
		} catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }
    }
}
